package com.paic.mhis.hcpms.productClear.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.productClear.biz.service.IProductClearService;
import com.paic.mhis.hcpms.productClear.dto.ProductClearDTO;
import com.paic.mhis.hcpms.productClear.dto.ProductClearParameterDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/productClearController/")
public class ProductClearController {
	@BizServicesProxy(beanName = "productClearServiceImpl")
	private IProductClearService productClearService;
	
	@RequestMapping(value = "loadProductClearData.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loadProductClearData(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = null;
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			ProductClearParameterDTO vo = JSONObject.toJavaObject(transferJson, ProductClearParameterDTO.class);
			
			result=this.productClearService.loadProductClearData(vo, user);
			
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
	
	@RequestMapping(value = "processProductClear.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> processProductClear(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = null;
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			ProductClearDTO bean = JSONObject.toJavaObject(transferJson, ProductClearDTO.class);
			this.productClearService.processProductClearData(bean, user);
			result=new HashMap<String,Object>();
			result.put("success","true");
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
	
	
	@RequestMapping(value = "batchProcessProductClear.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchProcessProductClear(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = null;
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			
			JSONArray arrayList = transferJson.getJSONArray("data");
			
			List<ProductClearDTO> beanList=new ArrayList<ProductClearDTO>();
			
			if (arrayList != null && arrayList.size() > 0) {

				for (Object value : arrayList) {

					JSONObject currentJson = JSON.parseObject(value.toString());
					ProductClearDTO bean = JSONObject.toJavaObject(currentJson, ProductClearDTO.class);
					

					beanList.add(bean);

				}

			}
			this.productClearService.batchProcessProductClearData(beanList, user);
			result=new HashMap<String,Object>();
			result.put("success","true");
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}


}
